package br.com.abril.sforce;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.topi.sharing.json.DateTimeDeserializer;
import com.topi.sharing.json.DateTimeSerializer;

/**
 * Entity implementation class for Entity: SalesForceProfile
 *
 */

@Entity
@Table(name = "SALES_FORCE_PROFILE")
public class SalesForceProfile implements Serializable {

	private static final long serialVersionUID = 1L;

	public enum ProfileStatus {
		ENABLED, DISABLED
	}
	

	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column(name = "LOGIN_URL", length = 300)
	private String loginUrl;

	@Column(name = "SERVICE_URL", length = 500)
	private String serviceUrl;

	@Column(name = "USER_NAME", length = 300)
	private String username;

	@Column(name = "PASSWORD", length = 100)
	private String password;

	@Column(name = "TOKEN", length = 400)
	private String token;

	@Column(name="SERVER_URL" , length = 300)
	private String serverUrl;
	
	@Column(name = "CLIENT_ID", length = 2000)
	private String clientId;

	// TODO VER SE PRECISA
	@Column(name = "CLIENT_SECRET", length = 2000)
	private String clientIdSecret;

	@Enumerated(EnumType.STRING)
	private ProfileStatus status;

	private boolean connected;

	@Column(length=400)
	private String sessionId;
	
	@JsonSerialize(using = DateTimeSerializer.class)
	@JsonDeserialize(using = DateTimeDeserializer.class)
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastConnection;
	
	@Column(length=4000)
	private String connectionStatusInfo;
	
	public SalesForceProfile() {
	}

	public ProfileStatus getStatus() {
		return status;
	}

	public void setStatus(ProfileStatus status) {
		this.status = status;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLoginUrl() {
		return loginUrl;
	}

	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}

	public String getServiceUrl() {
		return serviceUrl;
	}

	public void setServiceUrl(String serviceUrl) {
		this.serviceUrl = serviceUrl;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getClientIdSecret() {
		return clientIdSecret;
	}

	public void setClientIdSecret(String clientIdSecret) {
		this.clientIdSecret = clientIdSecret;
	}

	public URL getLoginURL() throws MalformedURLException {
		return new URL(this.loginUrl);
	}

	public URL getServiceURL() throws MalformedURLException {
		return new URL(this.serviceUrl);
	}

	public String getPasswordWithToken() {
		return this.password + this.token;
	}

	public String getServerUrl() {
		return serverUrl;
	}
	
	public void setServerUrl(String serverUrl) {
		this.serverUrl = serverUrl;
	}
	
	
	public boolean isConnected() {
		return connected;
	}

	public void setConnected(boolean connected) {
		this.connected = connected;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public Date getLastConnection() {
		return lastConnection;
	}

	public void setLastConnection(Date lastConnection) {
		this.lastConnection = lastConnection;
	}

	public String getConnectionStatusInfo() {
		return connectionStatusInfo;
	}
	
	public void setConnectionStatusInfo(String connectionStatusInfo) {
		this.connectionStatusInfo = StringUtils.left( connectionStatusInfo, 4000);
	}
	
	
	
	public void retriveServiceUrlFromReceivedConnection(String serviceUrl) {
		
		MessageFormat mf = new MessageFormat("https://{0}/services/Soap/u/{1}");
		
		try {
			Object [] content = mf.parse(serviceUrl);
			this.serverUrl = "https://" + content[0] + "/services/Soap/";
		} catch (Exception e) {
		}
		
	}
	
}
